const bodyParser = require("body-parser");
let { list } = require("./mock/index.js");
const Mock = require("mockjs");
const userList = require("./mock/user.json");
const JWT = require("jsonwebtoken");
const privateKey = "666666";
module.exports = app => {
  app.use(bodyParser.json());
  app.post("/api/login", (req, res) => {
    const { username, password } = req.body;
    const user = userList.find(
      v => v.username === username && v.password === password
    );
    if (user) {
      res.send({
        code: 200,
        data: {
          username: user.username,
          nickname: user.nickname,
          avator: user.avator,
          role: user.role,
          token: JWT.sign(
            {
              username: user.username,
              nickname: user.nickname,
              avator: user.avator,
              role: user.role,
            },
            privateKey
          ),
        },
        message: "登录成功， 欢迎用户" + user.nickname,
      });
    } else {
      res.send({
        code: 401,
        data: null,
        message: "账户密码错误， 请重新登录！！",
      });
    }
  });
  app.get("/api/user/info", (req, res) => {
    const { token } = req.query;
    try {
      const data = JWT.verify(token, privateKey);
      res.send({
        code: 200,
        message: "获取用户信息成功",
        data,
      });
    } catch {
      res.send({
        code: 403,
        message: "非法token, 请重新登录",
        data: null,
      });
    }
  });
  app.get("/api/list", (req, res) => {
    const { searchValue, type, desc } = req.query;
    let newList = [];
    if (searchValue) {
      newList = list.filter(v => {
        return (
          v.id.includes(searchValue) ||
          v.title.includes(searchValue) ||
          v.type.includes(searchValue) ||
          v.price.toString().includes(searchValue)
        );
      });
    } else {
      newList = [...list];
    }
    if (type && type !== "全部") {
      newList = newList.filter(v => v.type === type);
    }
    if (desc && desc !== "默认") {
      newList.sort((a, b) => {
        if (desc === "升序") return a.price - b.price;
        if (desc === "降序") return b.price - a.price;
      });
    }
    res.send({
      code: 200,
      data: newList,
    });
  });
  app.delete("/api/list/:id", (req, res) => {
    const { id } = req.params;
    list = list.filter(v => {
      return !id.includes(v.id);
    });
    res.send({
      code: 200,
      message: "删除成功",
    });
  });
  app.post("/api/list/:id", (req, res) => {
    const { id } = req.params;
    const item = list.find(v => v.id === id);
    if (item) {
      Object.assign(item, req.body);
      res.send({
        code: 200,
        message: "修改成功",
      });
    } else {
      res.send({
        code: 400,
        message: "修改失败",
      });
    }
  });
  app.post("/api/list", (req, res) => {
    req.body.id = Mock.Random.id();
    list.unshift(req.body);
    res.send({
      code: 200,
      message: "数据添加成功！！",
    });
  });
};
